import re
import numpy as np
import requests
import random


class RegexDict():
    def __init__(self,redict,judgekey):
        self.redict = redict
        self.judgekey = judgekey
    def main(self):
        for i in self.redict.keys():
            if re.search(i,self.judgekey):
                return self.redict[i]
        raise KeyError('not in dict')

class Tableone():
    def __init__(self,**params1):

        self.config = params1['config']
        self.get_money = params1['get_money']

    def table_get_data(self, data_dict, tag_ls):
        def get_all_title_text(all_title_text_dict, judge_y):
            if all_title_text_dict == {}:
                return '', ''

            ls = list(all_title_text_dict.keys())
            if judge_y in ls:
                if judge_y != ls[-1]:
                    return 'Gone', ls[ls.index(judge_y) + 1]
                else:
                    return 'Gone', ''
            ls.sort()

            if judge_y < int(ls[0]):
                all_title_text = ''
                all_title_text_later = all_title_text_dict[ls[0]]
            elif judge_y > int(ls[-1]):
                all_title_text = all_title_text_dict[ls[-1]]
                all_title_text_later = ''
            else:
                all_title_text = ls[0]
                all_title_text_later = ''
                for num, i in enumerate(ls):
                    if judge_y > int(i):
                        all_title_text = i
                        all_title_text_later = ls[num + 1]
                    else:
                        break
                all_title_text = all_title_text_dict[all_title_text]
                if all_title_text_later != '':
                    all_title_text_later = all_title_text_dict[all_title_text_later]
            return all_title_text, all_title_text_later

        end_use_dict = {}

        def change(i):
            if i[:2] == '数据':
                return i + '_' + str(random.randint(1, 10000))
            else:
                return i

        for tag_key in data_dict.keys():

            if tag_key[:4] == 'data':

                data_use_dict = data_dict[tag_key]
                all_title_text_dict = {}
                all_title_text_dict_ls_not_use = []
                #获取该table的title格式，逻辑为整行为同样的数据，{y_index:y_data}
                for all_title_text_key in data_use_dict.keys():
                    if (len(np.unique(data_use_dict[all_title_text_key]['x_data'])) == 1):
                        all_title_text_pre = data_use_dict[all_title_text_key]['x_data'][0]
                        if all_title_text_pre != '':
                            if data_use_dict[all_title_text_key]['y'] in all_title_text_dict_ls_not_use:
                                pass
                            else:
                                all_title_text_dict_ls_not_use.append(data_use_dict[all_title_text_key]['y'])
                                all_title_text_dict[data_use_dict[all_title_text_key]['y']] = all_title_text_pre
                title_index = [int(i) for i in list(all_title_text_dict.keys())]

                end_dict = {}
                start_x = 0
                start_y = 0
                end_x = -1
                end_y = -1
                first = {}
                first_index = {}
                pre_title_y = '-1'

                ol = data_use_dict.keys()
                use_key_num = 0
                no_keys = True

                for mul_data_key in ol:

                    direction = data_use_dict[mul_data_key]['direction']
                    if direction in ('right', 'down'):
                        no_keys = False
                        use_key_num += 1

                if no_keys == True:
                    end_use_dict[tag_key] = 'no keys'
                    continue
                for j, mul_data_key in enumerate(list(data_use_dict.keys())):
                    direction = data_use_dict[mul_data_key]['direction']
                    if direction == 'None':
                        continue
                    use_key_num -= 1

                    mul_data_value_ls_index = []
                    y = data_use_dict[mul_data_key]['y']

                    x = data_use_dict[mul_data_key]['x']
                    x_data = data_use_dict[mul_data_key]['x_data']
                    y_data = data_use_dict[mul_data_key]['y_data']

                    original = data_use_dict[mul_data_key]['original']
                    mul_data_key = mul_data_key.split('{')[0]
                    t_num_x = x
                    t_num_y = y

                    title_y, title_y_later = get_all_title_text(all_title_text_dict, y)

                    if title_y == 'Gone':
                        if mul_data_key[:4] != '中标金额':
                            continue
                        else:
                            title_y = ''
                    if re.search('^(标段|负责人|排名)$|^中标(公司|金额)[_-]?',title_y) != None:
                        title_y = title_y + '_title'
                    mul_data_value_ls = []
                    if title_y != '':
                        if title_y not in end_dict.keys():
                            end_dict[title_y] = {}

                    def title_tag(end_dict, title_y):
                        tt_search = re.search('(中标公司_)第?(([一二三四五六七八九十]){1,3})|(第?(([一二三四五六七八九十I]){1,3}|[ABCDEFGHIJKLMNOPQRSTUVWXYZ]))(中标候选人|标段?|包|合同段)', title_y)
                        if tt_search != None:
                            title_tag = tt_search.group()
                        else:
                            title_tag = ''
                        if title_tag != '':
                            ls = [i for i in end_dict[title_y]]
                            ls.insert(0,'title_tag')
                            end_dict[title_y]['title_tag'] = title_tag
                            end_dict[title_y] = {i:end_dict[title_y][i] for i in ls}
                        return end_dict

                    def store_data(mul_data_key, end_dict_key, mul_data_value_ls_index):

                        if mul_data_key not in end_dict_key:
                            end_dict_key[mul_data_key] = [(j,) for j in mul_data_value_ls_index[0]]
                        else:
                            rr = []
                            for i in range(3):
                                v = mul_data_value_ls_index[0][i]
                                v1 = end_dict_key.get(mul_data_key, [])[i]
                                c = (*v1, v)
                                rr.append(c)
                            end_dict_key[mul_data_key] = rr
                        return end_dict_key

                    def first_handle(pre_title_y, title_y, first, first_index, end_dict):

                        def temp(mul_key, end_dict_use, t_ls):

                            if mul_key not in end_dict_use.keys():
                                end_dict_use[mul_key] = t_ls
                            else:
                                c = t_ls
                                rr = []

                                for i in range(3):
                                    v = c[i]

                                    v1 = end_dict_use.get(mul_key, [])[i]
                                    rr.append((*v1, *v))

                                end_dict_use[mul_key] = rr
                            return end_dict_use

                        if (pre_title_y != title_y) and (pre_title_y != '-1'):
                            title_y = pre_title_y
                        tt_ls = []

                        for i in first[list(first.keys())[0]]:
                            tt_ls.append(i)
                        index_t = [first[list(first.keys())[0]].index(i) for i in tt_ls]
                        t_ls = []
                        tt = zip(*[first_index[list(first.keys())[0]][j] for j in index_t])
                        for l in tt:
                            t_ls.append(l)
                        if title_y != '':
                            end_dict[title_y] = temp(list(first.keys())[0], end_dict[title_y], t_ls)
                            end_dict = title_tag(end_dict, title_y)

                        else:

                            end_dict = temp(list(first.keys())[0], end_dict, t_ls)
                        return end_dict

                    def double_handle(pre_title_y, title_y, i, num, direction, end_dict, first, first_index):
                        if (pre_title_y != title_y) and (pre_title_y != '-1'):
                            title_y = pre_title_y
                        if title_y != '':

                            if ('数据' + str((i + num + 1)) + '_' + direction) in end_dict[title_y].keys():
                                end_dict[title_y]['数据' + str((i + num + 1)) + '_' + direction] = store_data(
                                    list(first.keys())[0],
                                    end_dict[title_y]['数据' + str((i + num + 1)) + '_' + direction],
                                    [first_index[list(first.keys())[0]][i]])
                            else:
                                end_dict[title_y]['数据' + str((i + num + 1)) + '_' + direction] = {}
                                end_dict[title_y]['数据' + str((i + num + 1)) + '_' + direction][
                                    list(first.keys())[0]] = [(j,) for j in first_index[list(first.keys())[0]][i]]
                            end_dict = title_tag(end_dict, title_y)
                        else:
                            if ('数据' + str((i + num + 1)) + '_' + direction) in end_dict.keys():
                                end_dict['数据' + str((i + num + 1)) + '_' + direction] = store_data(
                                    list(first.keys())[0], end_dict['数据' + str((i + num + 1)) + '_' + direction],
                                    [first_index[list(first.keys())[0]][i]])
                            else:
                                end_dict['数据' + str((i + num + 1)) + '_' + direction] = {}
                                end_dict['数据' + str((i + num + 1)) + '_' + direction][list(first.keys())[0]] = [(j,) for j in first_index[list(first.keys())[0]][i]]
                        return end_dict
                    def title_clear(end_dict,pre_title_y,title_y):
                        end_dict = {change(i): end_dict[i] for i in end_dict}
                        if pre_title_y in end_dict:
                            end_dict[pre_title_y] = {change(i): end_dict[pre_title_y][i] for i in end_dict[pre_title_y]}
                        if title_y in end_dict:
                            end_dict[title_y] = {change(i): end_dict[title_y][i] for i in end_dict[title_y]}
                        if (pre_title_y != title_y) and (pre_title_y != '-1'):
                            title_y = pre_title_y
                        return end_dict,title_y
                    money_unit = ''
                    if mul_data_key[:4] == '中标金额':
                        if '万元' in mul_data_key and '%' in mul_data_key:
                            money_unit = -1
                        elif '元' in mul_data_key and '%' in mul_data_key:
                            money_unit = -2
                        elif '万' in mul_data_key:
                            money_unit = 0
                        elif '%' in mul_data_key:
                            money_unit = 1
                        else:
                            money_unit = 2
                        mul_data_key = '中标金额'

                    if direction == 'right':

                        if mul_data_key[:4] == '中标公司':
                            ttt_ls = y_data
                            ttt_key = True
                            ttt_num = 0
                            for _ in ttt_ls:
                                if _[:4] == '中标金额' or _ == '负责人' or _ == '排名' or re.search('地址',_):
                                    ttt_key = True
                                    break
                                else:
                                    if re.search('评委|评分|得分|序号|未通过|废标|原因', _) != None:
                                        ttt_key = False
                                        ttt_num += 1

                            if ttt_key == False:
                                continue

                        for mul_data_value_x in x_data[x + 1:]:

                            if mul_data_value_x == '':
                                mul_data_value_x = '/'
                            if mul_data_key[:4] == '中标公司':
                                ttt_mul_data_key = '中标公司'
                            elif mul_data_key[:4] == '中标金额':
                                ttt_mul_data_key = '中标金额'
                            else:
                                ttt_mul_data_key = mul_data_key
                            if (mul_data_value_x not in tag_ls and re.search(RegexDict(self.config['need_keys_partone'],ttt_mul_data_key).main(),
                                mul_data_value_x) != None and y not in title_index) or \
                                    (mul_data_key == '排名' and re.search('中标公司_第[一二三四五]', mul_data_value_x) != None):

                                if (re.search(
                                        '^\d{1,20}\.?\d{0,20}（?万?元?）?$|[壹佰叁拾贰万贰仟捌佰元整]|^(\d{1,3}[,.]?){1,5}（?万?元?）?$',
                                        mul_data_value_x) != None) and (mul_data_key[:4] == '中标金额'):

                                    if '金额' in mul_data_value_x:
                                        mul_data_value_x = mul_data_value_x.split('金额')[1]
                                    if '元万元' in mul_data_value_x:
                                        mul_data_value_x = mul_data_value_x.replace('万元', '')
                                    if '总价' in mul_data_value_x:
                                        mul_data_value_x = re.split('总价:?',mul_data_value_x)[1]
                                    mul_data_value_x = re.sub('人民币|[，,：。￥]|CNY', '', mul_data_value_x)

                                    mul_data_value_x_ls = mul_data_value_x.split(' ')
                                    if len(mul_data_value_x_ls) > 1:
                                        mul_data_value_x = ''
                                        for mul_data_value_xi in mul_data_value_x_ls:
                                            try:
                                                float(re.sub('[零壹贰貳叁肆伍陆柒捌玖拾分角元拾佰百仟圆]','',mul_data_value_xi))
                                                mul_data_value_x += mul_data_value_xi
                                            except:
                                                continue
                                    mul_data_value_x_ls = re.findall('\d+\.?\d{0,}万?元', mul_data_value_x)
                                    mul_data_value_x_ = ''
                                    if len(mul_data_value_x_ls) > 1:
                                        for mul_data_value_x_lsi in mul_data_value_x_ls:
                                            mul_data_value_x_ += mul_data_value_x_lsi + '；'
                                        mul_data_value_x = mul_data_value_x_

                                    if '（' in mul_data_value_x and len(mul_data_value_x) <= 30:
                                        mul_data_value_x = mul_data_value_x.split('（')[0]
                                    if money_unit in (-1,-2):
                                        tt_len = len(mul_data_value_x) if '.' not in mul_data_value_x else len(mul_data_value_x.split('.')[0])
                                        if tt_len <= 3:
                                            money_unit = 1
                                        else:
                                            if money_unit == -1:
                                                money_unit = 0
                                            elif money_unit == -2:
                                                money_unit = 2

                                    if money_unit == 0:
                                        mul_data_value_x = mul_data_value_x.replace('万', '')
                                        try:
                                            ttt = str(float(self.get_money(mul_data_value_x)) * 10000)
                                        except:
                                            ttt = mul_data_value_x
                                    elif money_unit == 1:
                                        try:
                                            if '元' not in mul_data_value_x and float(mul_data_value_x) <= 100:
                                                ttt = str(float(self.get_money(mul_data_value_x)) * 10000) + '%'
                                            else:
                                                ttt = str(self.get_money(mul_data_value_x))
                                        except:
                                            ttt = mul_data_value_x
                                    elif money_unit == 2:
                                        if '万' in mul_data_value_x:
                                            try:
                                                ttt = str(self.get_money(mul_data_value_x)) + '_万'
                                            except:
                                                ttt = mul_data_value_x
                                        else:
                                            try:
                                                ttt = str(self.get_money(mul_data_value_x))
                                            except:
                                                ttt = mul_data_value_x

                                else:
                                    ttt = mul_data_value_x

                                if ttt == '0':
                                    ttt = '/'
                                mul_data_value_ls.append(ttt)
                                end_ls = []
                                t_num_x += 1
                                if mul_data_value_x + '{' + str(t_num_x) + '_' + str(y) + '}' in data_use_dict.keys():
                                    mul_data_value_x_ls = data_use_dict[mul_data_value_x + '{' + str(t_num_x) + '_' + str(y) + '}']['x_data'][:t_num_x] + [ttt]
                                else:
                                    mul_data_value_x_ls = [ttt]
                                mul_data_value_x_ls.reverse()
                                end_ls.append(mul_data_value_x_ls)
                                if mul_data_value_x + '{' + str(t_num_x) + '_' + str(y) + '}' in data_use_dict.keys():
                                    mul_data_value_x_ls = data_use_dict[mul_data_value_x + '{' + str(t_num_x) + '_' + str(y) + '}']['y_data'][:y] + [ttt]
                                else:
                                    mul_data_value_x_ls = [ttt]
                                mul_data_value_x_ls.reverse()
                                end_ls.append(mul_data_value_x_ls)
                                end_ls.append([original])
                                mul_data_value_ls_index.append((end_ls, t_num_x, y))
                            else:
                                break

                        if mul_data_value_ls == []:
                            continue

                        if use_key_num == 0:

                            if start_x == x and end_x == t_num_x:
                                if first != {}:
                                    for i, i_data in enumerate(first[list(first.keys())[0]]):
                                        end_dict = double_handle(pre_title_y, title_y, i, x, 'x', end_dict, first,first_index)
                                    first = {}
                                    first_index = {}
                                if len(mul_data_value_ls) == 1:
                                    if title_y != '':
                                        end_dict[title_y] = store_data(mul_data_key, end_dict[title_y],mul_data_value_ls_index)
                                        end_dict = title_tag(end_dict, title_y)
                                    else:
                                        end_dict = store_data(mul_data_key, end_dict, mul_data_value_ls_index)
                                else:
                                    for i, i_data in enumerate(mul_data_value_ls):
                                        if title_y != '':
                                            if ('数据' + str((i + x + 1)) + '_x') in end_dict[title_y].keys():
                                                end_dict[title_y]['数据' + str((i + x + 1)) + '_x'] = store_data(mul_data_key, end_dict[title_y]['数据' + str((i + x + 1)) + '_x'],
                                                    [mul_data_value_ls_index[i]])
                                            else:
                                                end_dict[title_y]['数据' + str((i + x + 1)) + '_x'] = {}
                                                end_dict[title_y]['数据' + str((i + x + 1)) + '_x'][mul_data_key] = [(j,)for j in mul_data_value_ls_index[i]]
                                            end_dict = title_tag(end_dict, title_y)
                                        else:
                                            if ('数据' + str((i + x + 1)) + '_x') in end_dict.keys():
                                                end_dict['数据' + str((i + x + 1)) + '_x'] = store_data(mul_data_key,end_dict['数据' + str((i + x + 1)) + '_x'],[mul_data_value_ls_index[i]])
                                            else:
                                                end_dict['数据' + str((i + x + 1)) + '_x'] = {}
                                                end_dict['数据' + str((i + x + 1)) + '_x'][mul_data_key] = [(j,) for j in mul_data_value_ls_index[i]]
                            else:

                                if first != {}:
                                    end_dict = first_handle(pre_title_y, title_y, first, first_index, end_dict)
                                    first = {}
                                    first_index = {}

                                end_dict,title_y = title_clear(end_dict, pre_title_y, title_y)

                                tt_ls = []
                                for i in mul_data_value_ls:
                                    if i not in tt_ls:
                                        tt_ls.append(i)
                                index_t = [mul_data_value_ls.index(i) for i in tt_ls]
                                t_ls = []
                                tt = zip(*[mul_data_value_ls_index[j] for j in index_t])
                                for l in tt:
                                    t_ls.append(l)

                                if title_y != '':
                                    if mul_data_key not in end_dict[title_y].keys():
                                        end_dict[title_y][mul_data_key] = t_ls
                                    else:
                                        c = t_ls
                                        rr = []
                                        for i in range(3):
                                            v = c[i]
                                            v1 = end_dict[title_y].get(mul_data_key, [])[i]
                                            rr.append((*v1, *v))
                                        end_dict[title_y][mul_data_key] = rr
                                    end_dict = title_tag(end_dict, title_y)

                                else:
                                    if mul_data_key not in end_dict.keys():
                                        end_dict[mul_data_key] = t_ls
                                    else:
                                        c = t_ls
                                        rr = []
                                        for i in range(3):
                                            v = c[i]
                                            v1 = end_dict.get(mul_data_key, [])[i]
                                            rr.append((*v1, *v))
                                        end_dict[mul_data_key] = rr
                            continue

                        if len(mul_data_value_ls) == 1:

                            if first != {}:
                                end_dict = first_handle(pre_title_y, title_y, first, first_index, end_dict)
                                first = {}
                                first_index = {}

                            if title_y != '':
                                end_dict[title_y] = store_data(mul_data_key, end_dict[title_y], mul_data_value_ls_index)
                                end_dict = title_tag(end_dict, title_y)
                            else:
                                end_dict = store_data(mul_data_key, end_dict, mul_data_value_ls_index)

                        elif len(np.unique(mul_data_value_ls)) != 1:

                            if first != {}:
                                if start_x == x and end_x == t_num_x:
                                    for i, i_data in enumerate(first[list(first.keys())[0]]):
                                        end_dict = double_handle(pre_title_y, title_y, i, x, 'x', end_dict, first,
                                                                 first_index)
                                    end_dict, title_y_nouse = title_clear(end_dict, pre_title_y, title_y)
                                else:
                                    end_dict = first_handle(pre_title_y, title_y, first, first_index, end_dict)
                                first = {}
                                first_index = {}
                            for i, i_data in enumerate(mul_data_value_ls):
                                if title_y != '':
                                    if ('数据' + str((i + x + 1)) + '_x') in end_dict[title_y].keys():
                                        end_dict[title_y]['数据' + str((i + x + 1)) + '_x'] = store_data(mul_data_key,
                                                                                                       end_dict[
                                                                                                           title_y][
                                                                                                           '数据' + str((
                                                                                                                                  i + x + 1)) + '_x'],
                                                                                                       [
                                                                                                           mul_data_value_ls_index[
                                                                                                               i]])
                                    else:
                                        end_dict[title_y]['数据' + str((i + x + 1)) + '_x'] = {}
                                        end_dict[title_y]['数据' + str((i + x + 1)) + '_x'][mul_data_key] = [(j,) for j in
                                                                                                           mul_data_value_ls_index[
                                                                                                               i]]
                                    end_dict = title_tag(end_dict, title_y)
                                else:
                                    if ('数据' + str((i + x + 1)) + '_x') in end_dict.keys():
                                        end_dict['数据' + str((i + x + 1)) + '_x'] = store_data(mul_data_key, end_dict[
                                            '数据' + str((i + x + 1)) + '_x'], [mul_data_value_ls_index[i]])
                                    else:
                                        end_dict['数据' + str((i + x + 1)) + '_x'] = {}
                                        end_dict['数据' + str((i + x + 1)) + '_x'][mul_data_key] = [(j,) for j in
                                                                                                  mul_data_value_ls_index[
                                                                                                      i]]


                        else:

                            if end_x == -1 or pre_title_y != title_y:

                                if first != {}:
                                    end_dict = first_handle(pre_title_y, title_y, first, first_index, end_dict)
                                    first = {}
                                    first_index = {}

                                    end_dict, title_y_nouse = title_clear(end_dict, pre_title_y, title_y)

                                first[mul_data_key] = mul_data_value_ls
                                first_index[mul_data_key] = mul_data_value_ls_index


                            else:

                                if start_x == x and end_x == t_num_x:

                                    if first != {}:

                                        for i, i_data in enumerate(first[list(first.keys())[0]]):
                                            end_dict = double_handle(pre_title_y, title_y, i, x, 'x', end_dict, first,
                                                                     first_index)
                                        first = {}
                                        first_index = {}

                                    for i, i_data in enumerate(mul_data_value_ls):
                                        if title_y != '':
                                            if ('数据' + str((i + x + 1)) + '_x') in end_dict[title_y].keys():
                                                end_dict[title_y]['数据' + str((i + x + 1)) + '_x'] = store_data(
                                                    mul_data_key, end_dict[title_y]['数据' + str((i + x + 1)) + '_x'],
                                                    [mul_data_value_ls_index[i]])
                                            else:
                                                end_dict[title_y]['数据' + str((i + x + 1)) + '_x'] = {}
                                                end_dict[title_y]['数据' + str((i + x + 1)) + '_x'][mul_data_key] = [(j,)
                                                                                                                   for j
                                                                                                                   in
                                                                                                                   mul_data_value_ls_index[
                                                                                                                       i]]
                                            end_dict = title_tag(end_dict, title_y)
                                        else:

                                            if ('数据' + str((i + x + 1)) + '_x') in end_dict.keys():

                                                end_dict['数据' + str((i + x + 1)) + '_x'] = store_data(mul_data_key,
                                                                                                      end_dict[
                                                                                                          '数据' + str((
                                                                                                                                 i + x + 1)) + '_x'],
                                                                                                      [
                                                                                                          mul_data_value_ls_index[
                                                                                                              i]])

                                            else:
                                                end_dict['数据' + str((i + x + 1)) + '_x'] = {}
                                                end_dict['数据' + str((i + x + 1)) + '_x'][mul_data_key] = [(j,) for j in
                                                                                                          mul_data_value_ls_index[
                                                                                                              i]]

                                else:

                                    if first != {}:
                                        end_dict = first_handle(pre_title_y, title_y, first, first_index, end_dict)

                                        first = {}
                                        first_index = {}

                                    end_dict, title_y_nouse = title_clear(end_dict, pre_title_y, title_y)

                                    first[mul_data_key] = mul_data_value_ls
                                    first_index[mul_data_key] = mul_data_value_ls_index



                        start_x = x
                        end_x = t_num_x

                        start_y = 0
                        end_y = -1

                    if direction == 'down':

                        if mul_data_key[:4] == '中标公司':
                            ttt_ls = x_data
                            ttt_key = True
                            ttt_num = 0
                            for _ in ttt_ls:
                                if _[:4] == '中标金额' or _ == '负责人' or _ == '排名' or re.search('地址',_):
                                    ttt_key = True
                                    break
                                else:
                                    if re.search('评委|评分|得分|序号|未通过|废标|原因', _) != None:
                                        ttt_key = False
                                        ttt_num += 1

                            if ttt_key == False:
                                continue

                        y_data_p = y if y <= 30 else 30
                        for p, mul_data_value_y in enumerate(y_data[y_data_p + 1:]):
                            if mul_data_value_y == '':
                                mul_data_value_y = '/'
                            if mul_data_key[:5] == '中标公司_' or mul_data_key[:5] == '中标公司-':
                                ttt_mul_data_key = '中标公司'
                            elif mul_data_key[:4] == '中标金额':
                                ttt_mul_data_key = '中标金额'
                            else:
                                ttt_mul_data_key = mul_data_key
                            if (mul_data_value_y not in tag_ls and re.search(RegexDict(self.config['need_keys_partone'],ttt_mul_data_key).main(),mul_data_value_y) != None and p + 1 + y not in title_index) or \
                                    (mul_data_key == '排名' and re.search('中标公司_第[一二三四五]', mul_data_value_y) != None):

                                if re.search('^\d{1,20}\.?\d{0,20}（?万?元?）?$|[壹佰叁拾贰万贰仟捌佰元整]|^(\d{1,3}[,.]?){1,5}（?万?元?）?$',
                                        mul_data_value_y) != None:

                                    if '金额' in mul_data_value_y:
                                        mul_data_value_y = mul_data_value_y.split('金额')[1]
                                    if '元万元' in mul_data_value_y:
                                        mul_data_value_y = mul_data_value_y.replace('万元', '')
                                    if '总价' in mul_data_value_y:
                                        mul_data_value_y = re.split('总价:?',mul_data_value_y)[1]
                                    mul_data_value_y = re.sub('人民币|[，,￥：。]|CNY', '', mul_data_value_y)
                                    mul_data_value_y_ls = mul_data_value_y.split(' ')
                                    if len(mul_data_value_y_ls) > 1:
                                        mul_data_value_y = ''
                                        for mul_data_value_yi in mul_data_value_y_ls:
                                            try:
                                                float(re.sub('[零壹贰貳叁肆伍陆柒捌玖拾分角元拾佰百仟圆]','',mul_data_value_yi))
                                                mul_data_value_y += mul_data_value_yi
                                            except:
                                                continue

                                    mul_data_value_y_ls = re.findall('\d+\.?\d{0,}万?元',mul_data_value_y)
                                    mul_data_value_y_ = ''
                                    if len(mul_data_value_y_ls) > 1:
                                        for mul_data_value_y_lsi in mul_data_value_y_ls:
                                            mul_data_value_y_ += mul_data_value_y_lsi + '；'
                                        mul_data_value_y = mul_data_value_y_

                                    if '（' in mul_data_value_y and len(mul_data_value_y) <= 30:
                                        mul_data_value_y = mul_data_value_y.split('（')[0]
                                    if money_unit == -1:
                                        if '.' not in mul_data_value_y:
                                            if len(mul_data_value_y) <= 3:
                                                money_unit = 1
                                            else:
                                                money_unit = 0
                                        else:
                                            if len(mul_data_value_y.split('.')[0]) <= 3:
                                                money_unit = 1
                                            else:
                                                money_unit = 0
                                    if money_unit == -2:
                                        if '.' not in mul_data_value_y:
                                            if len(mul_data_value_y) <= 3:
                                                money_unit = 1
                                            else:
                                                money_unit = 2
                                        else:
                                            if len(mul_data_value_y.split('.')[0]) <= 3:
                                                money_unit = 1
                                            else:
                                                money_unit = 2
                                    if money_unit == 0:
                                        mul_data_value_y = mul_data_value_y.replace('万', '')
                                        try:
                                            ttt = str(float(self.get_money(mul_data_value_y)) * 10000)
                                        except:
                                            ttt = mul_data_value_y
                                    elif money_unit == 1:
                                        try:
                                            if '元' not in mul_data_value_y and float(mul_data_value_y) <= 100:
                                                ttt = str(float(self.get_money(mul_data_value_y)) * 10000) + '%'
                                            else:
                                                ttt = str(self.get_money(mul_data_value_y))
                                        except:
                                            ttt = mul_data_value_y

                                    elif money_unit == 2:
                                        if '万' in mul_data_value_y:
                                            try:
                                                ttt = str(self.get_money(mul_data_value_y)) + '_万'
                                            except:
                                                ttt = mul_data_value_y
                                        else:
                                            try:
                                                ttt = str(self.get_money(mul_data_value_y))

                                            except:
                                                ttt = mul_data_value_y
                                    else:
                                        ttt = mul_data_value_y
                                else:
                                    ttt = mul_data_value_y

                                mul_data_value_ls.append(ttt)
                                t_num_y += 1
                                if mul_data_value_y + '{' + str(x) + '_' + str(t_num_y) + '}' in data_use_dict.keys():
                                    mul_data_value_y_ls = \
                                    data_use_dict[mul_data_value_y + '{' + str(x) + '_' + str(t_num_y) + '}']['x_data'][
                                    :t_num_x] + [ttt]
                                else:
                                    mul_data_value_y_ls = [ttt]
                                mul_data_value_y_ls.reverse()
                                mul_data_value_ls_index.append(([mul_data_value_y_ls, [ttt],[original]], x, t_num_y))
                            else:
                                break

                        if mul_data_value_ls == []:
                            if start_y == y and end_y == t_num_y:
                                if first != {}:
                                    for i, i_data in enumerate(first[list(first.keys())[0]]):
                                        end_dict = double_handle(pre_title_y, title_y, i, x, 'x', end_dict, first,
                                                                 first_index)
                                    first = {}
                                    first_index = {}
                            else:
                                end_dict, title_y_nouse = title_clear(end_dict, pre_title_y, title_y)
                                if first != {}:
                                    end_dict = first_handle(pre_title_y, title_y, first, first_index, end_dict)
                                    first = {}
                                    first_index = {}
                            continue

                        if use_key_num == 0:

                            if start_y == y and end_y == t_num_y:

                                if first != {}:
                                    for i, i_data in enumerate(first[list(first.keys())[0]]):
                                        end_dict = double_handle(pre_title_y, title_y, i, x, 'x', end_dict, first,
                                                                 first_index)
                                    first = {}
                                    first_index = {}
                                if len(mul_data_value_ls) == 1:
                                    if title_y != '':
                                        end_dict[title_y] = store_data(mul_data_key, end_dict[title_y],
                                                                       mul_data_value_ls_index)
                                        end_dict = title_tag(end_dict, title_y)
                                    else:
                                        end_dict = store_data(mul_data_key, end_dict, mul_data_value_ls_index)
                                else:
                                    for i, i_data in enumerate(mul_data_value_ls):
                                        if title_y != '':
                                            if ('数据' + str((i + y + 1)) + '_y') in end_dict[title_y].keys():
                                                end_dict[title_y]['数据' + str((i + y + 1)) + '_y'] = store_data(
                                                    mul_data_key, end_dict[title_y]['数据' + str((i + y + 1)) + '_y'],
                                                    [mul_data_value_ls_index[i]])
                                            else:
                                                end_dict[title_y]['数据' + str((i + y + 1)) + '_y'] = {}
                                                end_dict[title_y]['数据' + str((i + y + 1)) + '_y'][mul_data_key] = [(j,)for j in mul_data_value_ls_index[i]]
                                            end_dict = title_tag(end_dict, title_y)
                                        else:
                                            if ('数据' + str((i + y + 1)) + '_y') in end_dict.keys():
                                                end_dict['数据' + str((i + y + 1)) + '_y'] = store_data(mul_data_key,
                                                                                                      end_dict[
                                                                                                          '数据' + str((
                                                                                                                                 i + y + 1)) + '_y'],
                                                                                                      [
                                                                                                          mul_data_value_ls_index[
                                                                                                              i]])
                                            else:
                                                end_dict['数据' + str((i + y + 1)) + '_y'] = {}
                                                end_dict['数据' + str((i + y + 1)) + '_y'][mul_data_key] = [(j,) for j in
                                                                                                          mul_data_value_ls_index[
                                                                                                              i]]

                            else:

                                end_dict, title_y_nouse = title_clear(end_dict, pre_title_y, title_y)
                                if first != {}:
                                    end_dict = first_handle(pre_title_y, title_y, first, first_index, end_dict)
                                    first = {}
                                    first_index = {}

                                if (pre_title_y != title_y) and (pre_title_y != '-1'):
                                    title_y = pre_title_y
                                tt_ls = []
                                for i in mul_data_value_ls:
                                    if i not in tt_ls:
                                        tt_ls.append(i)

                                index_t = [mul_data_value_ls.index(i) for i in tt_ls]

                                t_ls = []
                                tt = zip(*[mul_data_value_ls_index[j] for j in index_t])
                                for l in tt:
                                    t_ls.append(l)

                                if title_y != '':
                                    if mul_data_key not in end_dict[title_y].keys():
                                        end_dict[title_y][mul_data_key] = t_ls
                                    else:
                                        c = t_ls
                                        rr = []
                                        for i in range(3):
                                            v = c[i]
                                            v1 = end_dict[title_y].get(mul_data_key, [])[i]
                                            rr.append((*v1, *v))
                                        end_dict[title_y][mul_data_key] = rr
                                    end_dict = title_tag(end_dict, title_y)
                                else:
                                    if mul_data_key not in end_dict.keys():
                                        end_dict[mul_data_key] = t_ls
                                    else:
                                        c = t_ls
                                        rr = []

                                        for i in range(3):
                                            v = c[i]
                                            v1 = end_dict.get(mul_data_key, [])[i]
                                            rr.append((*v1, *v))
                                        end_dict[mul_data_key] = rr
                            continue
                        if len(mul_data_value_ls) == 1:

                            if first != {}:
                                end_dict = first_handle(pre_title_y, title_y, first, first_index, end_dict)
                                first = {}
                                first_index = {}
                            if title_y != '':
                                end_dict[title_y] = store_data(mul_data_key, end_dict[title_y], mul_data_value_ls_index)
                                end_dict = title_tag(end_dict, title_y)
                            else:
                                end_dict = store_data(mul_data_key, end_dict, mul_data_value_ls_index)


                        elif len(np.unique(mul_data_value_ls)) != 1:
                            for i, i_data in enumerate(mul_data_value_ls):
                                if title_y != '':

                                    if ('数据' + str((i + y + 1)) + '_y') in end_dict[title_y].keys():
                                        end_dict[title_y]['数据' + str((i + y + 1)) + '_y'] = store_data(mul_data_key,
                                                                                                       end_dict[
                                                                                                           title_y][
                                                                                                           '数据' + str((
                                                                                                                                  i + y + 1)) + '_y'],
                                                                                                       [
                                                                                                           mul_data_value_ls_index[
                                                                                                               i]])
                                    else:
                                        end_dict[title_y]['数据' + str((i + y + 1)) + '_y'] = {}
                                        end_dict[title_y]['数据' + str((i + y + 1)) + '_y'][mul_data_key] = [(j,) for j in
                                                                                                           mul_data_value_ls_index[
                                                                                                               i]]
                                    end_dict = title_tag(end_dict, title_y)
                                else:
                                    if ('数据' + str((i + y + 1)) + '_y') in end_dict.keys():
                                        end_dict['数据' + str((i + y + 1)) + '_y'] = store_data(mul_data_key, end_dict[
                                            '数据' + str((i + y + 1)) + '_y'], [mul_data_value_ls_index[i]])
                                    else:
                                        end_dict['数据' + str((i + y + 1)) + '_y'] = {}
                                        end_dict['数据' + str((i + y + 1)) + '_y'][mul_data_key] = [(j,) for j in
                                                                                                  mul_data_value_ls_index[
                                                                                                      i]]
                            if first != {}:
                                if start_y == y and end_y == t_num_y:
                                    for i, i_data in enumerate(first[list(first.keys())[0]]):
                                        end_dict = double_handle(pre_title_y, title_y, i, y, 'y', end_dict, first,
                                                                 first_index)
                                    end_dict, title_y_nouse = title_clear(end_dict, pre_title_y, title_y)
                                else:
                                    end_dict = first_handle(pre_title_y, title_y, first, first_index, end_dict)
                                first = {}
                                first_index = {}

                        else:

                            if end_y == -1 or pre_title_y != title_y:
                                first[mul_data_key] = mul_data_value_ls
                                first_index[mul_data_key] = mul_data_value_ls_index

                            else:
                                if start_y == y and end_y == t_num_y:

                                    if first != {}:
                                        for i, i_data in enumerate(first[list(first.keys())[0]]):
                                            end_dict = double_handle(pre_title_y, title_y, i, x, 'x', end_dict, first,
                                                                     first_index)
                                        first = {}
                                        first_index = {}

                                    for i, i_data in enumerate(mul_data_value_ls):
                                        if title_y != '':
                                            if ('数据' + str((i + y + 1)) + '_y') in end_dict[title_y].keys():
                                                end_dict[title_y]['数据' + str((i + y + 1)) + '_y'] = store_data(
                                                    mul_data_key, end_dict[title_y]['数据' + str((i + y + 1)) + '_y'],
                                                    [mul_data_value_ls_index[i]])
                                            else:
                                                end_dict[title_y]['数据' + str((i + y + 1)) + '_y'] = {}
                                                end_dict[title_y]['数据' + str((i + y + 1)) + '_y'][mul_data_key] = [(j,)
                                                                                                                   for j
                                                                                                                   in
                                                                                                                   mul_data_value_ls_index[
                                                                                                                       i]]
                                            end_dict = title_tag(end_dict, title_y)
                                        else:
                                            if ('数据' + str((i + y + 1)) + '_y') in end_dict.keys():
                                                end_dict['数据' + str((i + y + 1)) + '_y'] = store_data(mul_data_key,
                                                                                                      end_dict[
                                                                                                          '数据' + str((
                                                                                                                                 i + y + 1)) + '_y'],
                                                                                                      [
                                                                                                          mul_data_value_ls_index[
                                                                                                              i]])
                                            else:
                                                end_dict['数据' + str((i + y + 1)) + '_y'] = {}
                                                end_dict['数据' + str((i + y + 1)) + '_y'][mul_data_key] = [(j,) for j in
                                                                                                          mul_data_value_ls_index[
                                                                                                              i]]

                                else:
                                    if first != {}:
                                        end_dict = first_handle(pre_title_y, title_y, first, first_index, end_dict)
                                        first = {}
                                        first_index = {}

                                    if pre_title_y in end_dict:
                                        end_dict[pre_title_y] = {change(i): end_dict[pre_title_y][i] for i in
                                                                 end_dict[pre_title_y]}
                                    if title_y in end_dict:
                                        end_dict[title_y] = {change(i): end_dict[title_y][i] for i in
                                                             end_dict[title_y]}

                                    first[mul_data_key] = mul_data_value_ls
                                    first_index[mul_data_key] = mul_data_value_ls_index
                                    end_dict, title_y_nouse = title_clear(end_dict, pre_title_y, title_y)

                        start_y = y
                        end_y = t_num_y
                        start_x = 0
                        end_x = -1


                    pre_title_y = title_y
                end_use_dict[tag_key] = end_dict

            elif tag_key[:5] == 'table':
                end_use_dict[tag_key] = self.table_get_data(data_dict[tag_key], tag_ls)

        return end_use_dict

    def clear_solo_data(self, end_dict):
        mm = list(end_dict.keys())
        for i in mm:
            if isinstance(end_dict[i], dict) and i[:2] != '数据':
                end_dict[i] = self.clear_solo_data(end_dict[i])
            else:
                if i[:2] == '数据':
                    if len(end_dict[i]) == 1:
                        for j in end_dict[i]:
                            if isinstance(end_dict.get(j, []),dict):
                                continue
                            if j not in end_dict:
                                end_dict[j] = end_dict[i][j]
                            else:
                                rr = []
                                for m in range(3):
                                    v = end_dict.get(j, [])[m]
                                    v1 = end_dict[i][j][m]
                                    rr.append((*v, *v1))
                                end_dict[j] = rr
                        del end_dict[i]
        return end_dict

    def end(self, end_dict_pre):

        def clear_mul_data(end_dict):
            end_dict_keys = list(end_dict.keys())
            base = {}
            base_key = ''
            for i in end_dict_keys:
                if i[:2] == '数据' or i[:3] == 'mul':
                    if end_dict[i] == base and base_key != '':
                        del end_dict[base_key]
                    base = end_dict[i]
                    base_key = i
            return end_dict

        def belong(base, x, y):
            belong_num = 0
            for i, j in enumerate(base):
                if x >= j[0] and y >= j[1]:
                    belong_num = i
                else:
                    break
            return belong_num

        def tag_insert(use_dict,key,title_tag):
            def tag_find(use_dict,use_dict_tag,tag_all):
                for tag_key, tag in enumerate(use_dict_tag):
                    if re.search(self.config['tag_word_partone'], tag):
                        if tag_key != len(use_dict_tag) - 1:
                            obj = re.search(
                                '(([一二三四五六七八九十\d]){1,3})(?=标?[段包]|合同段|中标)）?|标段([一二三四五六七八九十\d]{1,3})|包件?([一二三四五六七八九十\d]{1,3})',
                                use_dict_tag[tag_key + 1])
                            if obj:
                                if '标段' not in use_dict['mul' + str(j)]:
                                    use_dict['mul' + str(j)]['标段'] = obj.group()
                            obj = re.search('(([一二三四五六七八九十\d]){1,3})名',use_dict_tag[tag_key + 1])
                            if obj:
                                if '排名' not in use_dict['mul' + str(j)]:
                                    use_dict['mul' + str(j)]['排名'] = obj.group()
                            tag_all = tag
                            break
                        else:
                            tag_all = tag
                return tag_all,use_dict
            for j in range(len(use_dict[key][0])):

                use_dict['mul' + str(j)] = {}
                use_dict['mul' + str(j)][key] = use_dict[key][0][j][0][0]
                use_dict['mul' + str(j)]['original_'+key] = use_dict[key][0][j][2][0]
                tag_all = ''
                tag_all,use_dict = tag_find(use_dict, use_dict[key][0][j][0][1:],tag_all)
                if tag_all == '':
                    tag_all, use_dict = tag_find(use_dict,use_dict[key][0][j][1][1:],tag_all)
                if tag_all != '':
                    use_dict['mul' + str(j)]['tag'] = tag_all
                    if title_tag != tag_all:
                        if '公司' in title_tag and '排名' not in use_dict['mul' + str(j)]:
                            use_dict['mul' + str(j)]['排名'] = re.search('(中标公司_)?(第[一二三四五六七八九十])(中标候选人)?',title_tag).group(2)
                        elif '标段' in title_tag and '标段' not in use_dict['mul' + str(j)]:
                            use_dict['mul' + str(j)]['标段'] = re.search('(第?[一二三四五六七八九十]|[ABCDEFGHIJKLMNOPQRSTUVWXYZ])(标段|合同段)?', title_tag).group(1)
                if title_tag != '':
                    if tag_all == '':
                        use_dict['mul' + str(j)]['tag'] = title_tag
                    else:
                        use_dict['mul' + str(j)]['title_tag'] = title_tag
            return use_dict

        def main_use(use_dict,title_tag):

            key_value_not_dict = []
            key_value_is_dict = []
            if use_dict == 'no keys':
                return 'no keys'

            for mul_value_key in use_dict:
                if isinstance(use_dict[mul_value_key], dict):
                    key_value_is_dict.append(mul_value_key)
                    continue
                else:
                    if mul_value_key == 'title_tag':
                        title_tag = use_dict['title_tag']
                        continue
                    key_value_not_dict.append(mul_value_key)
            max_mul_num = 0
            company_order = False
            company_no_order = False
            company_order_ls = []
            for i in key_value_not_dict:
                if use_dict[i] == []:
                    continue
                max_mul_num = max(max_mul_num, len(use_dict[i][2]))
            if max_mul_num == 1:
                for i in key_value_not_dict:

                    use_dict['original_' + i] = use_dict[i][0][0][2][0]
                    tag_all = ''
                    for tag_key,tag in enumerate(use_dict[i][0][0][0][1:]):
                        if re.search(self.config['tag_word_partone'], tag):
                            if tag_key != len(use_dict[i][0][0][0][1:]) - 1:
                                obj = re.search(
                                    '(([一二三四五六七八九十\d]){1,3})(?=标?[段包]|合同段|中标)）?|标段([一二三四五六七八九十\d]{1,3})|包件?([一二三四五六七八九十\d]{1,3})',
                                    use_dict[i][0][0][0][1:][tag_key + 1])
                                if obj:
                                    if '标段' not in use_dict:
                                        use_dict['标段'] = obj.group()
                                obj = re.search('([一二三四五六七八九十\d]){1,3}名',use_dict[i][0][0][0][1:][tag_key + 1])
                                if obj:
                                    if '排名' not in use_dict:
                                        use_dict['排名'] = obj.group()
                                tag_all = tag
                                break
                            else:
                                tag_all = tag
                                break
                    if tag_all == '':
                        for tag_key, tag in enumerate(use_dict[i][0][0][1][1:]):
                            if re.search(self.config['tag_word_partone'], tag):
                                if tag_key != len(use_dict[i][0][0][1][1:]) - 1:
                                    obj = re.search(
                                        '(([一二三四五六七八九十\d]){1,3})(?=名|标?[段包]|合同段|中标)）?|标段([一二三四五六七八九十\d]{1,3})|包件?([一二三四五六七八九十\d]{1,3})',
                                        use_dict[i][0][0][1][1:][tag_key + 1])
                                    if obj:
                                        if '标段' not in use_dict:
                                            use_dict['标段'] = obj.group()
                                    obj = re.search('([一二三四五六七八九十\d]){1,3}名',use_dict[i][0][0][1][1:][tag_key + 1])
                                    if obj:
                                        if '排名' not in use_dict:
                                            use_dict['排名'] = obj.group()
                                    tag_all = tag
                                    break
                                else:
                                    tag_all = tag
                                    break
                    if tag_all != '':
                        use_dict['tag'] = tag_all
                        if title_tag != tag_all:
                            if '公司' in title_tag and '排名' not in use_dict:
                                use_dict['排名'] = re.search('(中标公司_)?(第[一二三四五六七八九十])(中标候选人)?', title_tag).group(2)
                            elif '标段' in title_tag and '标段' not in use_dict:
                                use_dict['标段'] = re.search('(第?[一二三四五六七八九十]|[ABCDEFGHIJKLMNOPQRSTUVWXYZ])(标段|合同段)?', title_tag).group(1)

                    if title_tag != '':
                        if tag_all == '':
                            use_dict['tag'] = title_tag
                        else:
                            use_dict['title_tag'] = title_tag
                    use_dict[i] = use_dict[i][0][0][0][0]


                for i in key_value_is_dict:

                    tt = main_use(use_dict[i],title_tag)
                    use_dict[i] = tt
                use_dict = clear_mul_data(use_dict)

                return use_dict

            for i in key_value_not_dict:
                if i[:5] == '中标公司_' and use_dict[i] != []:
                    company_order = True
                    company_order_ls.append({i: use_dict[i]})
                    del use_dict[i]
                elif (i  == '中标公司'  or i[:5] == '中标公司-') and use_dict[i] != []:
                    company_no_order = True
                else:
                    pass

            base_start = []

            if company_no_order == True:

                if company_order == True:
                    for i in company_order_ls:
                        for j in i:
                            use_dict[j] = i[j][0][0][0][0]
                            use_dict['original_'+j] = i[j][0][0][2][0]
                if '中标公司' in use_dict:
                    base_start_ = zip(use_dict['中标公司'][1], use_dict['中标公司'][2])
                    base_start = []
                    for i in base_start_:
                        base_start.append(i)
                    use_dict = tag_insert(use_dict, '中标公司',title_tag)
                    del use_dict['中标公司']
                    if '中标公司-especial' in use_dict:
                        del use_dict['中标公司-especial']
                else:
                    base_start_ = zip(use_dict['中标公司-especial'][1], use_dict['中标公司-especial'][2])
                    base_start = []
                    for i in base_start_:
                        base_start.append(i)
                    use_dict = tag_insert(use_dict, '中标公司-especial',title_tag)
                    del use_dict['中标公司-especial']



            elif company_order == True and company_no_order == False:

                base_start = [(list(i.values())[0][1][0], list(i.values())[0][2][0]) for i in company_order_ls]
                for j, i in enumerate(company_order_ls):
                    use_dict['mul' + str(j)] = {}
                    use_dict['mul' + str(j)][list(i.keys())[0]] = list(i.values())[0][0][0][0][0]
                    use_dict['mul' + str(j)]['original_'+list(i.keys())[0]] = list(i.values())[0][0][0][2][0]

                    tag_all = ''
                    for tag_key,tag in enumerate(list(i.values())[0][0][0][0][1:]):
                        if re.search(self.config['tag_word_partone'], tag):
                            if tag_key != len(list(i.values())[0][0][0][0][1:]) - 1:
                                obj = re.search(
                                    '(([一二三四五六七八九十\d]){1,3})(?=标?[段包]|合同段|中标)）?|标段([一二三四五六七八九十\d]{1,3})|包件?([一二三四五六七八九十\d]{1,3})',
                                    list(i.values())[0][0][0][0][1:][tag_key + 1])
                                if obj:
                                    if '标段' not in use_dict['mul' + str(j)]:
                                        use_dict['mul' + str(j)]['标段'] = obj.group()
                                obj = re.search('([一二三四五六七八九十\d]){1,3}名',list(i.values())[0][0][0][0][1:][tag_key + 1])
                                if obj:
                                    if '排名' not in use_dict['mul' + str(j)]:
                                        use_dict['mul' + str(j)]['排名'] = obj.group()
                                tag_all = tag
                                break
                            else:
                                tag_all = tag
                    if tag_all == '':
                        for tag_key, tag in enumerate(list(i.values())[0][0][0][1][1:]):
                            if re.search(self.config['tag_word_partone'], tag):
                                if tag_key != len(list(i.values())[0][0][0][1][1:]) - 1:
                                    obj = re.search(
                                        '(([一二三四五六七八九十\d]){1,3})(?=标?[段包]|合同段|中标)）?|标段([一二三四五六七八九十\d]{1,3})|包件?([一二三四五六七八九十\d]{1,3})',
                                        list(i.values())[0][0][0][1][1:][tag_key + 1])
                                    if obj:
                                        if '标段' not in use_dict['mul' + str(j)]:
                                            use_dict['mul' + str(j)]['标段'] = obj.group()
                                    obj = re.search('([一二三四五六七八九十\d]){1,3}名',list(i.values())[0][0][0][1][1:][tag_key + 1])
                                    if obj:
                                        if '排名' not in use_dict['mul' + str(j)]:
                                            use_dict['mul' + str(j)]['排名'] = obj.group()
                                    tag_all = tag
                                    break
                                else:
                                    tag_all = tag
                                    break
                    if tag_all != '':
                        use_dict['mul' + str(j)]['tag'] = tag_all
                        if title_tag != tag_all:
                            if '公司' in title_tag and '排名' not in use_dict['mul' + str(j)]:
                                use_dict['mul' + str(j)]['排名'] = re.search('(中标公司_)?(第[一二三四五六七八九十])(中标候选人)?',title_tag).group(2)
                            elif '标段' in title_tag and '标段' not in use_dict['mul' + str(j)] :
                                use_dict['mul' + str(j)]['标段'] = re.search('(第?[一二三四五六七八九十]|[ABCDEFGHIJKLMNOPQRSTUVWXYZ])(标段|合同段)?', title_tag).group(1)

                    if title_tag != '':
                        if tag_all == '':
                            use_dict['mul' + str(j)]['tag'] = title_tag
                        else:
                            use_dict['mul' + str(j)]['title_tag'] = title_tag
            if company_order == False and company_no_order == False:

                for i in key_value_not_dict:
                    if use_dict[i] == []:
                        continue
                    for j in range(len(use_dict[i][0])):
                        if 'mul' + str(j) not in use_dict:
                            use_dict['mul' + str(j)] = {}

                        use_dict['mul' + str(j)][i] = use_dict[i][0][j][0][0]
                        use_dict['mul' + str(j)]['original_'+i] = use_dict[i][0][j][2][0]

                        tag_all = ''
                        for tag_key,tag in enumerate(use_dict[i][0][j][0][1:]):
                            if re.search(self.config['tag_word_partone'], tag):
                                if tag_key != len(use_dict[i][0][j][0][1:]) - 1:
                                    obj = re.search(
                                        '(([一二三四五六七八九十\d]){1,3})(?=标?[段包]|合同段|中标)）?|标段([一二三四五六七八九十\d]{1,3})|包件?([一二三四五六七八九十\d]{1,3})',
                                        use_dict[i][0][j][0][1:][tag_key + 1])
                                    if obj:
                                        if '标段' not in use_dict['mul' + str(j)]:
                                            use_dict['mul' + str(j)]['标段'] = obj.group()
                                    obj = re.search('([一二三四五六七八九十\d]){1,3}名',use_dict[i][0][j][0][1:][tag_key + 1])
                                    if obj:
                                        if '排名' not in use_dict['mul' + str(j)]:
                                            use_dict['mul' + str(j)]['排名'] = obj.group()
                                    tag_all = tag
                                    break
                                else:
                                    tag_all = tag
                                    break
                        if tag_all == '':
                            for tag_key, tag in enumerate(use_dict[i][0][j][1][1:]):
                                if re.search(self.config['tag_word_partone'], tag):
                                    if tag_key != len(use_dict[i][0][j][1][1:]) - 1:
                                        obj = re.search(
                                            '(([一二三四五六七八九十\d]){1,3})(?=标?[段包]|中标|合同段)）?|标段([一二三四五六七八九十\d]{1,3})|包件?([一二三四五六七八九十\d]{1,3})',
                                            use_dict[i][0][j][1][1:][tag_key + 1])
                                        if obj:
                                            if '标段' not in use_dict['mul' + str(j)]:
                                                use_dict['mul' + str(j)]['标段'] = obj.group()
                                        obj = re.search('([一二三四五六七八九十\d]){1,3}名',
                                            use_dict[i][0][j][1][1:][tag_key + 1])
                                        if obj:
                                                if '排名' not in use_dict['mul' + str(j)]:
                                                    use_dict['mul' + str(j)]['排名'] = obj.group()
                                        tag_all = tag
                                        break
                                    else:
                                        tag_all = tag
                                        break
                        if tag_all != '':
                            use_dict['mul' + str(j)]['tag'] = tag_all
                            if title_tag != tag_all:
                                if '公司' in title_tag and '排名' not in use_dict['mul' + str(j)]:
                                    use_dict['mul' + str(j)]['排名'] = re.search('(中标公司_)?(第[一二三四五六七八九十])(中标候选人)?',title_tag).group(2)
                                elif '标段' in title_tag and '标段' not in use_dict['mul' + str(j)]:
                                    use_dict['mul' + str(j)]['标段'] = re.search('(第?[一二三四五六七八九十]|[ABCDEFGHIJKLMNOPQRSTUVWXYZ])(标段|合同段)?', title_tag).group(1)

                        if title_tag != '':
                            if tag_all == '':
                                use_dict['mul' + str(j)]['tag'] = title_tag
                            else:
                                use_dict['mul' + str(j)]['title_tag'] = title_tag
                    del use_dict[i]


            else:
                x_y_base = base_start
                for i in key_value_not_dict:
                    if i[:4] == '中标公司':
                        continue
                    for j in range(len(use_dict[i][0])):
                        belong_num = belong(x_y_base, use_dict[i][1][j], use_dict[i][2][j])
                        if i not in use_dict['mul' + str(belong_num)]:
                            use_dict['mul' + str(belong_num)][i] = use_dict[i][0][j][0][0]
                            use_dict['mul' + str(belong_num)]['original_'+i] = use_dict[i][0][j][2][0]
                            tag_all = ''
                            for tag_key,tag in enumerate(use_dict[i][0][j][0][1:]):
                                if re.search(self.config['tag_word_partone'], tag):
                                    if tag_key != len(use_dict[i][0][j][0][1:]) - 1:
                                        obj = re.search(
                                            '(([一二三四五六七八九十\d]){1,3})(?=标?[段包]|中标|合同段)）?|标段([一二三四五六七八九十\d]{1,3})|包件?([一二三四五六七八九十\d]{1,3})',
                                            use_dict[i][0][j][0][1:][tag_key + 1])
                                        if obj:
                                            if '标段' not in use_dict['mul' + str(belong_num)]:
                                                use_dict['mul' + str(belong_num)]['标段'] = obj.group()
                                        obj = re.search('(([一二三四五六七八九十\d]){1,3})名',use_dict[i][0][j][0][1:][tag_key + 1])
                                        if obj:
                                            if '排名' not in use_dict['mul' + str(belong_num)]:
                                                use_dict['mul' + str(belong_num)]['排名'] = obj.group()
                                        tag_all = tag
                                        break
                                    else:
                                        tag_all = tag
                                        break
                            if tag_all == '':
                                for tag_key, tag in enumerate(use_dict[i][0][j][1][1:]):
                                    if re.search(self.config['tag_word_partone'],tag):
                                        if tag_key != len(use_dict[i][0][j][1][1:]) - 1:
                                            obj = re.search(
                                                '(([一二三四五六七八九十\d]){1,3})(?=标?[段包]|中标|合同段)）?|标段([一二三四五六七八九十\d]{1,3})|包件?([一二三四五六七八九十\d]{1,3})',
                                                use_dict[i][0][j][1][1:][tag_key + 1])
                                            if obj:
                                                if '标段' not in use_dict['mul' + str(belong_num)]:
                                                    use_dict['mul' + str(belong_num)]['标段'] = obj.group()
                                            obj = re.search('(([一二三四五六七八九十\d]){1,3})名',use_dict[i][0][j][1][1:][tag_key + 1])
                                            if obj:
                                                if '排名' not in use_dict['mul' + str(belong_num)]:
                                                    use_dict['mul' + str(belong_num)]['排名'] = obj.group()
                                            tag_all = tag
                                            break
                                        else:
                                            tag_all = tag
                                            break
                            if tag_all != '':
                                use_dict['mul' + str(belong_num)]['tag'] = tag_all
                                if title_tag != tag_all:
                                    if '公司' in title_tag and '排名' not in use_dict['mul' + str(belong_num)]:
                                        use_dict['mul' + str(belong_num)]['排名'] = re.search('(中标公司_)?(第[一二三四五六七八九十])(中标候选人)?',
                                                                                      title_tag).group(2)
                                    elif '标段' in title_tag and '标段' not in use_dict['mul' + str(belong_num)]:
                                        use_dict['mul' + str(belong_num)]['标段'] = re.search('(第?[一二三四五六七八九十]|[ABCDEFGHIJKLMNOPQRSTUVWXYZ])(标段|合同段)?', title_tag).group(1)

                            if title_tag != '':
                                if tag_all == '':
                                    use_dict['mul' + str(belong_num)]['tag'] = title_tag
                                else:
                                    use_dict['mul' + str(belong_num)]['title_tag'] = title_tag

                    del use_dict[i]

            for i in key_value_is_dict:

                tt = main_use(use_dict[i],title_tag)
                use_dict[i] = tt
            use_dict = clear_mul_data(use_dict)
            # use_dict = clear_solo_data(use_dict)

            return use_dict

        end_use_dict = {}
        for tag_key in end_dict_pre.keys():
            if tag_key[:4] == 'data':
                data_use_dict = end_dict_pre[tag_key]
                end_use_dict[tag_key] = main_use(data_use_dict,'')
            elif tag_key[:5] == 'table':
                end_use_dict[tag_key] = self.end(end_dict_pre[tag_key])
        return end_use_dict


    """清除不需要的titletag下的数据"""
    def clear_title_tag(self,end_dict):
        def main_use(data_use_dict):
            if data_use_dict == 'no keys':
                return data_use_dict
            new_dict = data_use_dict.copy()
            for i in data_use_dict:
                if isinstance(data_use_dict[i],dict) and i[:2] != '数据' and i[:3] != 'mul':
                    title_delete = '评分情况'
                    if re.search(title_delete,i):
                        del new_dict[i]
            return new_dict


        for tag_key in end_dict.keys():
            if tag_key[:4] == 'data':
                data_use_dict = end_dict[tag_key]
                end_dict[tag_key] = main_use(data_use_dict)
            elif tag_key[:5] == 'table':
                end_dict[tag_key] = self.clear_title_tag(end_dict[tag_key])
        return end_dict
    """获取html网页，传入为url"""

    def get_html(self, geturl):
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'}

        res = requests.get(geturl, timeout=5, headers=headers)

        if res.encoding == 'ISO-8859-1':
            res.encoding = res.apparent_encoding
        else:
            res.encoding = 'utf-8'
        return res.text

    def to_end_visible(self, end_dict):

        visible_ls_end = []

        def main_use(data_use_dict):
            visible_ls = []
            temp_visible_dict = {}
            no_dict_keys = []
            yes_dict_keys = []
            for i in data_use_dict.keys():
                if isinstance(data_use_dict[i], dict):
                    yes_dict_keys.append(i)
                else:
                    no_dict_keys.append(i)

            for i in no_dict_keys:
                if re.search(self.config['need_key_str_partone'], i):
                    temp_visible_dict[i] = data_use_dict[i]

            if temp_visible_dict != {}:
                if len(temp_visible_dict) > 1:
                    tt_dict = {}
                    for m in temp_visible_dict:
                        if (m != 'tag' and m != 'title_tag') and ('中标金额' in temp_visible_dict and m[:5] == '中标金额_'):
                            tt_dict[m] = temp_visible_dict[m]
                    tt_key = False
                    tt_values = list(tt_dict.values())

                    for i in range(len(tt_values) - 1):
                        base = tt_values[i]
                        for j in range(i + 1, len(tt_values)):
                            if base == tt_values[j]:
                                tt_key = True
                                break

                    if tt_key == False:

                        tt = list(temp_visible_dict.keys())
                        tt = [m.split('_')[0] if m.split('_')[0] not in ('中标金额','original') else m for m in tt]

                        if len(np.unique(tt)) != len(tt):
                            tt = {}
                            for n in temp_visible_dict:
                                if n != '中标公司':
                                    tt[n] = temp_visible_dict[n]
                            visible_ls += [tt]
                        else:
                            visible_ls.append(temp_visible_dict)
                    else:
                        tt = list(temp_visible_dict.keys())
                        tt = [m.split('_')[0] if m.split('_')[0] != '中标金额' else m for m in tt]
                        if len(np.unique(tt)) != len(tt):
                            tt = {}
                            for n in temp_visible_dict:
                                if n != '中标公司':
                                    tt[n] = temp_visible_dict[n]
                            visible_ls += [tt]
                else:
                    visible_ls.append(temp_visible_dict)

            for i in yes_dict_keys:
                visible_ls += main_use(data_use_dict[i])

            return visible_ls

        for tag_key in end_dict.keys():
            if tag_key[:4] == 'data':
                data_use_dict = end_dict[tag_key]
                if data_use_dict == 'no keys':
                    continue
                # visible_concat_data = self.visible_concat(main_use(data_use_dict))

                visible_ls_end = visible_ls_end + main_use(data_use_dict)

            elif tag_key[:5] == 'table':

                visible_ls_end = visible_ls_end + self.to_end_visible(end_dict[tag_key])

        return visible_ls_end

    def visible_concat(self, visible_dict):

        visible_end_ls = []
        lone_ls = []
        lone_ls_no_tag = []
        # 拆分中标公司_第一
        visible_dict_ = []
        for i, j in enumerate(visible_dict):
            # 根据tag清洗especial中标公司
            if 'tag' in j:
                if j['tag'][:6] == '中标公司_第' and '中标公司' not in j and '中标公司-especial' in j:
                    j['中标公司'] = j['中标公司-especial']
                    del j['中标公司-especial']
            tt_dict = j.copy()
            tt_ls = []
            for e in j:

                if '中标公司_' in e and 'original' not in e:
                    if '中标公司' not in tt_dict:
                        tt_dict['中标公司'] = j[e]
                        tt_dict['tag'] = e.split('_')[1]
                        del tt_dict[e]
                    elif tt_dict['中标公司'] != j[e]:
                        e_t = 'original_' + e
                        tt_ls.append({'中标公司':j[e],'tag':e.split('_')[1],e_t:j[e_t]})
                        del tt_dict[e]
                        del tt_dict[e_t]
                else:
                    pass
            visible_dict_.append(tt_dict)
            visible_dict_ += tt_ls
        visible_dict = visible_dict_


        #根据中标公司划分
        tt_dict = {}
        for i in visible_dict:
            t_key = False
            key_ls = []
            for t_keyi in list(i.keys()):
                if re.search('^中标公司',t_keyi):
                    t_key = True
                    key_ls.append(t_keyi)
            if t_key == True:
                if '中标公司' in key_ls:
                    tt_dict[i['中标公司']] = tt_dict.get(i['中标公司'], []) + [i]
                else:
                    for _ in key_ls:
                        tt_dict[i[_]] = tt_dict.get(i[_], []) + [i]
        visible_end_ls_temp_end = []
        for p in tt_dict:
            visible_end_ls_temp = []
            p_ls = tt_dict[p]
            t_key = False
            t_key1 = False
            for t_keyi in p_ls:
                if '中标公司' in t_keyi:
                    t_key = True
                if '中标公司-especial' in t_keyi:
                    t_key1 = True
            if t_key * t_key1 == 1:
                for t_keyi in p_ls:
                    if '中标公司-especial' in t_keyi:
                        if '中标公司' not in t_keyi:
                            p_ls.remove(t_keyi)
                        else:
                            del t_keyi['中标公司-especial']
            if p_ls == []:
                continue
            if len(p_ls) == 1:
                visible_end_ls_temp.append(p_ls[0])
                visible_end_ls_temp_end += visible_end_ls_temp
                continue
            for i in p_ls[1:]:
                aaa = sorted(list(i.keys()))
                bbb = sorted(list(p_ls[0].keys()))
                if len(i) == 1:
                    pass
                elif aaa == bbb:
                    t_key = False
                    for _ in aaa:
                        if i[_] != p_ls[0][_]:
                            t_key = True
                    if t_key == True:
                        visible_end_ls_temp.append(i)
                else:
                    for j in i.keys():
                        if j == '中标公司':
                            pass
                        else:
                            if j not in p_ls[0]:
                                p_ls[0][j] = i[j]
                            elif j == '排名' and re.search('[1一]',i[j]) and re.search('[1一]',p_ls[0][j]) == None:
                                p_ls[0][j] = i[j]
            visible_end_ls_temp.insert(0, p_ls[0])
            visible_end_ls_temp_end += visible_end_ls_temp
        visible_end_ls = visible_end_ls_temp_end

        title_tag = ''
        visible_dict_ = []
        #将title_tag内化到每一个数据中去
        for i in visible_dict:
            if 'title_tag' in i and len(i) == 1:
                title_tag = i['title_tag']
            else:
                if title_tag != '':
                    i['title_tag'] = title_tag
                visible_dict_.append(i)
        visible_dict = visible_dict_

        company_spe = []
        company = False
        for j,i in enumerate(visible_dict):
            if '中标公司-especial' in i:
                company_spe.append(j)
            if '中标公司' in i:
                company = True
        visible_dict_ = []

        visible_end_ls_ = visible_end_ls.copy()
        if company == True:
            for companyi in range(len(visible_dict)):
                if companyi in company_spe:
                    continue
                else:
                    visible_dict_.append(visible_dict[companyi])
            for companyi in visible_end_ls:
                if '中标公司-especial' in companyi:
                    visible_end_ls_.remove(companyi)
        else:
            visible_dict_ = visible_dict
        visible_dict = visible_dict_

        visible_end_ls = visible_end_ls_

        for i in visible_dict:
            if 'tag' in i:
                lone_ls.append(i)
            elif '中标公司' not in i:
                lone_ls_no_tag.append(i)

        if lone_ls != []:
            tt_key = False
            for num, j in enumerate(visible_end_ls):
                if 'tag' in j:
                    tt_key = True

            if tt_key == True:
                for i in lone_ls:
                    tag = i['tag']
                    update_dict = i
                    for num, j in enumerate(visible_end_ls):
                        if 'tag' not in j or j == 'title_tag':
                            continue
                        a = re.search('(第[一二三四五六七八九十])', tag)
                        b = re.search('(第[一二三四五六七八九十])', j['tag'])
                        if a != None:
                            a_group = a.group(1)
                        else:
                            a_group = ''
                        if b != None:
                            b_group = b.group(1)
                        else:
                            b_group = ''
                        if 'tag' in j and (j['tag'] == tag or (a_group != '' and a_group == b_group)):
                            for _ in update_dict:
                                if _ not in j:
                                    j[_] = update_dict[_]
            else:
                min_num = min(len(lone_ls), len(visible_end_ls))
                for i in range(min_num):
                    for j in lone_ls[i]:
                        if j in visible_end_ls[i]:
                            continue
                        else:
                            visible_end_ls[i][j] = lone_ls[i][j]

        for i in lone_ls_no_tag:
            for m in i:
                if m in ('总分', '投标报价分'):
                    continue
                for num, j in enumerate(visible_end_ls):
                    if m in j:
                        continue
                    else:
                        j.update({m: i[m]})
                        visible_end_ls[num] = j
                        break

        tt_ls = []
        for i in visible_end_ls:
            if i not in tt_ls:
                tt_ls.append(i)
        visible_end_ls = tt_ls

        tt_ls = []
        for i in range(len(visible_end_ls) - 1):
            base = visible_end_ls[i]
            base_key = list(base.keys())
            for j in range(i + 1, len(visible_end_ls)):
                compare_key = list(visible_end_ls[j].keys())
                a = [1 if i in base else 0 for i in compare_key]
                b = [1 if i in compare_key else 0 for i in base]

                if sum(a) == len(compare_key):

                    t = False
                    for _ in compare_key:
                        if visible_end_ls[j][_] != base[_]:
                            t = True
                    if t == False:
                        tt_ls.append(visible_end_ls[j])
                elif sum(b) == len(base):
                    t = False
                    for _ in base_key:
                        if visible_end_ls[j][_] != base[_]:
                            t = True
                    if t == False:
                        tt_ls.append(base)
                    base = visible_end_ls[j]
                else:
                    pass

        visible_end_ls_ = visible_end_ls.copy()
        for i in visible_end_ls:
            if i in tt_ls:
                visible_end_ls_.remove(i)
        visible_end_ls = visible_end_ls_

        return visible_end_ls

    def tag_finish(self, visible_end_ls):

        for i in range(len(visible_end_ls)):

            tt = visible_end_ls[i]
            if '中标金额_especial' in tt:
                if '中标金额' in tt:
                    del tt['中标金额_especial']
                else:
                    tt['中标金额'] = tt['中标金额_especial']
                    del tt['中标金额_especial']

            if 'tag' in tt:
                if re.search('^第?[一二三四五六七八九十]名?|第\d名', tt['tag']) != None and re.search('[包标段]', tt['tag']) == None:
                    if '排名' not in tt:
                        tt['排名'] = tt['tag']
                    del tt['tag']
                elif re.search('中标公司_(第[一二三四五六七八九十])', tt['tag']) != None:
                    if '排名' not in tt:
                        tt['排名'] = re.search('中标公司_(第[一二三四五六七八九十])', tt['tag']).group(1)
                    del tt['tag']

                elif re.search('^\d{1,2}$', tt['tag']) == None and re.search('候选人', tt['tag']) == None:
                    tt['标段'] = tt['tag']
                    del tt['tag']
            if 'title_tag' in tt:
                if re.search('^第?[一二三四五六七八九十]名?|第\d名', tt['title_tag']) != None and re.search('[包标段]', tt['title_tag']) == None:
                    tt['排名'] = tt['title_tag']
                    del tt['title_tag']
                elif re.search('中标公司_(第[一二三四五六七八九十])', tt['title_tag']) != None:
                    tt['排名'] = re.search('中标公司_(第[一二三四五六七八九十])', tt['title_tag']).group(1)
                    del tt['title_tag']

                elif re.search('^\d{1,2}$', tt['title_tag']) == None and re.search('候选人', tt['title_tag']) == None:
                    tt['标段'] = tt['title_tag']
                    del tt['title_tag']
            visible_end_ls[i] = tt
        return visible_end_ls

    def rank_to_tag(self, visible_dict):

        for i, j in enumerate(visible_dict):
            if '排名' in j and 'tag' not in j:
                tt = re.search('(第[一二三四五六七八九十\d])', j['排名'])
                if tt != None:
                    j['tag'] = tt.group(1)
            if 'tag' in j:
                tt = re.search('(第?[一二三四五六七八九十\d])', j['tag'])

                if tt != None and re.search('[包标段件]', j['tag']) == None:
                    j['tag'] = tt.group(1)
                tag_dict = {'0':'零','1':'一','2':'二','3':'三','4':'四','5':'五','6':'六','7':'七','8':'八','9':'九','10':'十'}
                b = re.search('(\d)',j['tag'])
                if b != None:
                    j['tag'] = '第' + tag_dict[b.group(1)]
            if '标段' in j:
                tag_dict = {'1':'一','2':'二','3':'三','4':'四','5':'五','6':'六','7':'七','8':'八','9':'九','0':''}
                b = re.search('(\d{1,3})(包件?|标段)',j['标段'])

                if b != None:
                    num_word = b.group(1)

                    if len(num_word) == 1:
                        num_word = num_word
                    elif len(num_word) == 2:
                        num_word = tag_dict[num_word[0]] + '十' + tag_dict[num_word[1]]

                    else:
                        num_word = tag_dict[num_word[0]] + '百' + tag_dict[num_word[1]] + '十' + tag_dict[num_word[1]]
                    if '包' in j['标段']:
                        temp_word = '包'
                    else:
                        temp_word = '标段'
                    j['标段'] = num_word + temp_word

        return visible_dict

    def number_one(self, visible_concat_data):
        tt = []
        for i in visible_concat_data:
            if '中标公司' in i:
                if i['中标公司'] == '/':
                    continue
            tt.append(i)
        biao_ls = []
        visible_concat_data = tt
        tt = []
        end_tt = []

        for i in visible_concat_data:
            if '排名' in i:
                if re.search('^1&|^第?[一1]名?$', i['排名']) != None:
                    end_tt.append(i)
            elif '序号' in i:
                if i['序号'] == '1':
                    end_tt.append(i)
            else:
                tt.append(i)

        if end_tt != []:
            if len(end_tt) == 1:
                return [end_tt[0]]
            else:
                if '标段' not in end_tt[0]:
                    end_tt[0]['标段'] = ''
                gg = [end_tt[0]]
                biao_ls = [end_tt[0]['标段']]
                for i in end_tt[1:]:
                    if '标段' not in i:
                        pass
                    else:
                        for g in end_tt[1:]:
                            if '标段' in g and g['标段'] not in biao_ls:
                                gg.append(g)
                                biao_ls.append(g['标段'])

                return gg

        elif tt != []:

            if len(tt) == 1:
                return [tt[0]]
            else:
                if '标段' not in tt[0]:
                    tt[0]['标段'] = ''
                gg = [tt[0]]
                biao_ls = [tt[0]['标段']]
                for i in tt[1:]:
                    if '标段' not in i:
                        pass
                    else:
                        for g in tt[1:]:
                            if '标段' in g and g['标段'] not in biao_ls:
                                gg.append(g)
                                biao_ls.append(g['标段'])
                return gg
        elif end_tt == [] and tt == [] and visible_concat_data != []:
            return [visible_concat_data[0]]
        else:
            return []

    def money_type(self, number_one_data):

        for i, j in enumerate(number_one_data):
            if '价格类型' not in j:
                if '中标金额' in j:
                    if '_' in j['中标金额']:
                        j['中标金额'] = j['中标金额'].split('_')[0]
            else:
                if '中标金额' in j:
                    if '万' in j['价格类型']:
                        if '_' not in j['中标金额']:
                            j['中标金额'] = j['中标金额'].split('_')[0]
                            try:
                                j['中标金额'] = str(float(j['中标金额']) * 10000)
                            except:
                                pass
                        else:
                            j['中标金额'] = j['中标金额'].split('_')[0]
                    else:
                        if '_' in j['中标金额']:
                            j['中标金额'] = j['中标金额'].split('_')[0]
                del j['价格类型']
        return number_one_data

    def supply(self, number_one_data):
        for i in number_one_data:
            if '中标公司-especial' in i:
                i['中标公司'] = i['中标公司-especial']
                del i['中标公司-especial']
            for j in ['中标公司', '中标金额', '负责人', '标段','项目负责人资格','项目负责人证书编号']:
                if j in i:
                    continue
                else:
                    i[j] = '' if j != '中标金额' else 0
        return number_one_data
    def change_key_name(self,number_one_data):
        def change(word):
            if word == '中标公司':
                return '第一中标候选人'
            elif word == '负责人':
                return '项目负责人'
            elif word == 'original_中标公司':
                return 'original_第一中标候选人'
            elif word == 'original_负责人':
                return 'original_项目负责人'
            else:
                return word
        for i_num,i in enumerate(number_one_data):
            number_one_data[i_num] = {change(j):i[j] for j in i}
        return number_one_data

    def remove_nouse_origianl(self,number_one_data):
        for num,i in enumerate(number_one_data):
            temp_dict = i.copy()
            for j in i:
                if 'original' in j:
                    if (j.split('_')[1] not in i) or (j.split('_')[1] == i[j]) :
                        del temp_dict[j]
            number_one_data[num] = temp_dict
        return number_one_data


    def handle_mp(self,number_one_data):
        for i in number_one_data:
            #清洗项目负责人和资格连在一起的情况
            pm_qualification = ''
            pm_qualification_number = ''
            if '项目负责人' in i:
                if i['项目负责人'] in ['无','无无无']:
                    i['项目负责人'] = ''
                i['项目负责人'] = re.sub('[（）]|姓名：?|(证书|注册)编号|执业证书信息','',i['项目负责人'])
                obj = re.search('([一二三四五六七八九十壹贰貳叁肆伍陆柒捌玖拾高]级.*|(园林|建筑|水利水电|工程师).*)',i['项目负责人'])
                if obj != None:
                    pm_qualification = obj.group()
                    i['项目负责人'] = i['项目负责人'].split(obj.group()[0])[0]
                #清洗负责人资格
                if ('项目负责人资格' not in i or i['项目负责人资格'] in ['/','']) and pm_qualification != '':
                    i['项目负责人资格'] = pm_qualification

                obj = re.search('(([\d京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽黔贵粤青藏川宁琼])\d+.*)',i['项目负责人'])
                if obj != None:
                    pm_qualification_number = obj.group()
                    i['项目负责人'] = i['项目负责人'].split(obj.group()[0])[0]
                if ('项目负责人证书编号' not in i or i['项目负责人证书编号'] in ['/', '']) and pm_qualification_number != '':
                    i['项目负责人证书编号'] = pm_qualification_number

                #联合三者
                if '项目负责人资格' not in i:
                    pm_qualification_end = ''
                else:
                    pm_qualification_end = '_' + i['项目负责人资格']
                if '项目负责人证书编号' not in i:
                    pm_qualification_number_end = ''
                else:
                    pm_qualification_number_end = '_' + i['项目负责人证书编号']
                i['项目负责人_资格_编号'] = i['项目负责人']+pm_qualification_end+pm_qualification_number_end
        return number_one_data

    def order(self,visible_concat_data):
        no = []
        yes = []
        visible_concat_data_ = visible_concat_data.copy()
        for i in visible_concat_data:
            if '中标公司' in i:
                if i['中标公司'] in ('/','--'):
                    visible_concat_data_.remove(i)
            elif '中标公司-especial' in i:
                if i['中标公司-especial'] in ('/','--'):
                    visible_concat_data_.remove(i)

        visible_concat_data = visible_concat_data_
        for i in visible_concat_data:
            if '投标报价分' not in i and '总分' not in i:
                no.append(i)
            else:
                if '总分' in i:
                    yes.append(i)
                    i['grade'] = i['总分']
                else:
                    yes.append(i)
                    i['grade'] = i['投标报价分']

        if no != []:
            return no
        else:
            try:
                yes = sorted(yes, key=lambda x: x['grade'], reverse=True)
            except:
                pass
            return yes
    def record_judge(self,visible_dict,record_key):
        if record_key == False:
            return False
        else:
            end_judge = True
            for i in visible_dict:
                if '中标公司' in i or '中标公司_第一' in i:
                    end_judge = False
                    break
            if end_judge == True:
                return True
            else:
                return  False

    def end_data_clear(self,number_one_data):
        for i in number_one_data:
            if '中标金额' in i:
                if i['中标金额'] == 0:
                    continue
                num_judge_obj = re.search('(?<!\.)(\d+)\.?',i['中标金额'])
                if num_judge_obj:
                    num_judge = num_judge_obj.group(1)
                    if len(num_judge) >= 12:
                        i['中标金额'] = 0
        return number_one_data
    def ge(self,number_one_data):
        out = {}
        out['第一中标候选人'] = []
        out['第二中标候选人'] = []
        out['第三中标候选人'] = []
        for i in number_one_data:
            out['第一中标候选人'].append({'公司名称':i['第一中标候选人'],'中标金额':i['中标金额'],'项目负责人':i['项目负责人'],'项目负责人资格':i['项目负责人资格'],'项目负责人证书编号':i['项目负责人证书编号'],'项目负责人_资格_编号':i['项目负责人_资格_编号'],'标段':i['标段']})
            del i['中标金额'],i['项目负责人'],i['项目负责人资格'],i['项目负责人证书编号'],i['项目负责人_资格_编号'],i['标段']
        return out


    def part_one_table(self, data_dict, tag_ls,record_key):
        end_dict_pre = self.table_get_data(data_dict, tag_ls)
        end_dict_pre = self.clear_solo_data(end_dict_pre)
        # end_dict = ''
        end_dict = self.end(end_dict_pre)
        end_dict = self.clear_title_tag(end_dict)
        visible_dict = self.to_end_visible(end_dict)
        visible_dict = self.rank_to_tag(visible_dict)

        #判断是否是开标记录
        if self.record_judge(visible_dict,record_key):
            return ['record']
        visible_concat_data = self.visible_concat(visible_dict)
        visible_concat_data = self.tag_finish(visible_concat_data)
        visible_concat_data = self.order(visible_concat_data)
        number_one_data = self.number_one(visible_concat_data)
        number_one_data = self.money_type(number_one_data)
        number_one_data = self.supply(number_one_data)
        number_one_data = self.change_key_name(number_one_data)
        number_one_data = self.handle_mp(number_one_data)

        number_one_data = self.remove_nouse_origianl(number_one_data)
        number_one_data = self.end_data_clear(number_one_data)
        number_one_data = self.ge(number_one_data)
        if number_one_data == 'no table':
            number_one_data = ['no table']

        return number_one_data